package com.lovo.roomProject.dao;

import com.lovo.roomProject.pojo.SystemDO;

import java.sql.*;

public abstract class BaseDAO {
        protected Connection con;
        protected PreparedStatement statement;
        protected ResultSet rs;
        protected boolean autoCommit = true;

        public Connection getConnection() {
// 1. 加载驱动
            // con.isClosed(),如果这个连接被关闭了，就会返回true
            try {
                if (con == null || con.isClosed()) {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    // 2. 创建连接
                    con = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/room?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                            "root", "926445");
                    System.out.println("con:" + con);
                }
            } catch (ClassNotFoundException | SQLException throwables) {
                throwables.printStackTrace();
            }
            return con;
        }

        public void close() {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

        public void close(Connection con, Statement statement) {
            if (statement != null && autoCommit) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null && autoCommit) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

        public void close(Connection con, Statement statement, ResultSet rs) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null && autoCommit) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null && autoCommit) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

        public void setAutoCommit(boolean autoCommit) {
            try {
                this.autoCommit = autoCommit;
                this.getConnection();
                con.setAutoCommit(autoCommit);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        public void commit() {
            try {
                con.commit();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        public void rollback() {
            try {
                con.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
}
